<template>
  <div>
    <el-dialog
      title="新增订阅配置"
      :visible.sync="dialogVisible"
      width="500px"
      :before-close="closeAddDialog"
      :close-on-click-modal="false"
    >
      <el-form
        :model="addForm"
        :rules="rules"
        ref="refAddForm"
        label-width="100px"
      >
        <el-form-item size="small">
          <el-radio v-model="radio" label="1">单个新增</el-radio>
          <el-radio v-model="radio" label="2" style="margin-left: 20px"
            >批量新增</el-radio
          >
        </el-form-item>

        <el-form-item
          size="small"
          label="组织机构"
          prop="targetId"
          v-if="radio == '1'"
        >
          <el-select
            size="small"
            style="width: 100%"
            v-loadMore="loadMore"
            v-model="addForm.targetId"
            placeholder="请选择组织机构"
          >
            <el-option
              v-for="item in projectList"
              :label="item.orgName"
              :value="item.id"
              :key="item.id"
            >
            </el-option>
            <div
              class="div_box"
              v-loading="loading"
              :style="loading ? '' : 'display: none;'"
            ></div>
          </el-select>
        </el-form-item>

        <el-form-item
          size="small"
          label="组织机构"
          prop="targetId"
          v-show="radio == '2'"
        >
          <el-select
            size="small"
            style="width: 100%"
            v-loadMore="loadMore"
            v-model="addForm.targetId"
            placeholder="请选择组织机构"
            multiple
            collapse-tags
          >
            <el-option
              v-for="item in projectList"
              :label="item.orgName"
              :value="item.id"
              :key="item.id"
            >
            </el-option>
            <div
              class="div_box"
              v-loading="loading"
              :style="loading ? '' : 'display: none;'"
            ></div>
          </el-select>
        </el-form-item>

        <el-form-item size="small" label="事件类型" prop="event">
          <el-select
            v-model="addForm.event"
            multiple
            collapse-tags
            placeholder="请选择事件类型"
            style="width: 100%"
          >
            <el-option
              v-for="item in eventList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item size="small" label="URL" prop="url">
          <el-input
            clearable
            v-model.trim="addForm.url"
            placeholder="请输入URL"
            maxlength="100"
            style="width: 100%"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeAddDialog" class="cancel-button">取 消</el-button>
        <el-button
          size="small"
          @click="addSubmit"
          v-preventReClick="1000" class="confirm-button"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
  
<script>
import { reqSubscribeadd, reqOrgList, reqSubscribebatchAdd } from "@api/api";
import { mapState } from "vuex";
import loadMore from "@mixin/loadMore";

export default {
  mixins: [loadMore],
  components: {},
  data() {
    return {
      dialogVisible: false,
      radio: "1",
      addForm: {
        targetId: "",
        targetId2: "",
        url: "",
        event: "",
      },
      eventList: [
        {
          id: 1,
          name: "设备状态更新",
        },
        {
          id: 2,
          name: "告警事件",
        },
        {
          id: 3,
          name: "绑定事件",
        },
        {
          id: 4,
          name: "解绑事件",
        },
      ],
      // 表单验证规则
      rules: {
        url: [
          {
            required: true,
            message: "请输入URL",
            trigger: "blur",
          },
        ],
        targetId: [
          {
            required: true,
            message: "请选择组织机构",
            trigger: ["blur", "change"],
          },
        ],
        targetId2: [
          {
            required: true,
            message: "请选择组织机构",
            trigger: ["blur", "change"],
          },
        ],
        event: [
          {
            required: true,
            message: "请选择事件类型",
            trigger: ["blur", "change"],
          },
        ],
      },

      projectList: [], //组织机构数据
      sysInstanceIndex: 1,
      sysInstanceTotalPages: 10,
      loading: false,
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  watch: {
    dialogVisible(e) {
      if (e) {
        this.$nextTick(() => {
          this.closeAddDialog2();
        });
      }
    },
    radio(e) {
      this.addForm.targetId = "";
    },
  },
  methods: {
    async loadMore() {
      if (this.sysInstanceIndex >= Math.ceil(this.sysInstanceTotalPages / 10))
        return;
      this.sysInstanceIndex++;
      this.loading = true;
      // 获取列表数据的方法
      await this.OrgList();
      this.loading = false;
    },
    // 获取组织机构列表
    async OrgList(flag) {
      var res = await reqOrgList( {
        userId: this.user.userId,
        roleName: this.user.roleName,
        tenantId: this.user.tenantId,
      });
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      // if (flag) {
      //     this.addForm.targetId = res.data.rows[0].id || "";
      // }
      this.projectList =res.data.rows
    },

    // 点击新增
    addSubmit() {
      this.$refs.refAddForm.validate(async (valid) => {
        if (!valid) return;
        if (this.radio == "1") {
          const res = await reqSubscribeadd({
            url: this.addForm.url,
            event: this.addForm.event,
            targetId: this.addForm.targetId,
            tenantId: this.user.tenantId,
          });
          if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
        } else {
          const res = await reqSubscribebatchAdd({
            url: this.addForm.url,
            event: this.addForm.event,
            targetIdList: this.addForm.targetId,
            tenantId: this.user.tenantId,
          });
          if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
        }

        this.$modal.notifySuccess("新增订阅配置成功");
        this.closeAddDialog();
        this.$parent.getDataList();
      });
    },
    // 点击关闭弹框
    closeAddDialog() {
      this.$refs.refAddForm.resetFields();
      this.dialogVisible = false;
      this.projectList = []; //组织机构数据
      this.sysInstanceIndex = 1;
      this.sysInstanceTotalPages = 10;
      this.radio = "1";
    },
    closeAddDialog2() {
      this.$refs.refAddForm.resetFields();
    },
  },
  mounted() {},
  deactivated() {
    if(this.$refs.refAddForm!=null){
      this.closeAddDialog();
    }
  },
};
</script>
  
<style lang="less" scoped>
@import url("../../../../common.less");

// 批量按钮开始
::v-deep .el-radio {
  color: @primaryThemeTxtColor2;
}

::v-deep .el-radio__input.is-checked + .el-radio__label {
  color: #7367f0;
}

::v-deep .el-radio__input.is-checked .el-radio__inner {
  border-color: #7367f0;
  background: #7367f0;
}

// 批量按钮结束
</style>
  